/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EditMenuList.java
 *
 * Created on Oct 23, 2010, 3:44:22 PM
 */

package cat20032010;
import java.sql.*;
import java.util.Vector;
import java.util.logging.*;
import javax.swing.JOptionPane;
/**
 *
 * @author Daniel Tan
 */
public class EditMenuList extends javax.swing.JFrame {
    Statement stmt;
    private ResultSet rset;
    static public String newordername,newordercode,neworderprice;
    public static Vector vecMenu = new Vector();

    String oldcode = "";
    String oldname = "";
    String oldprice ="";
    String newcode = "";
    String newname = "";
    String newprice ="";
    /** Creates new form EditMenuList */
    public EditMenuList() {
        initComponents();
        setLocationRelativeTo(null);
        MenuList();


    }
    public void reset(){
        menucode.setText("");
        menuname.setText("");
        menuprice.setText("");
    }
public void MenuList(){
    vecMenu = new Vector();
    vecMenu.add("Menu Code");
        try {
            Main.getConnection();
            stmt = Main.con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
            rset = stmt.executeQuery("SELECT Menu_Code as Code from Menu");
        } catch (SQLException ex) {
            System.out.println("wrong liao");
        }
        try {
            while (rset.next()) {
                vecMenu.add(rset.getString(1));

            }
        } catch (SQLException ex) {
            System.out.println("Can't Get String");
        }
        MenuCodeBox.setModel(new javax.swing.DefaultComboBoxModel(vecMenu));


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        menuname = new javax.swing.JTextField();
        menuprice = new javax.swing.JTextField();
        menucode = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        MenuCodeBox = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setLayout(null);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel1.setText(" Edit Menu List");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(165, 25, 176, 42);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel2.setText("RM");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(230, 220, 30, 30);

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel3.setText("New Menu Code");
        jPanel1.add(jLabel3);
        jLabel3.setBounds(60, 140, 130, 30);

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel4.setText("New Menu Name");
        jPanel1.add(jLabel4);
        jLabel4.setBounds(60, 180, 130, 30);

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel5.setText("New Menu Price");
        jPanel1.add(jLabel5);
        jLabel5.setBounds(60, 220, 130, 30);

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel6.setText(":");
        jPanel1.add(jLabel6);
        jLabel6.setBounds(190, 140, 40, 30);

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel7.setText(":");
        jPanel1.add(jLabel7);
        jLabel7.setBounds(190, 180, 40, 30);
        jPanel1.add(menuname);
        menuname.setBounds(230, 180, 190, 30);
        jPanel1.add(menuprice);
        menuprice.setBounds(260, 220, 160, 30);
        jPanel1.add(menucode);
        menucode.setBounds(230, 140, 190, 30);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/button/OK.png"))); // NOI18N
        jButton1.setBorderPainted(false);
        jButton1.setContentAreaFilled(false);
        jButton1.setDefaultCapable(false);
        jButton1.setFocusPainted(false);
        jButton1.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/button/OKMouseOver.png"))); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1);
        jButton1.setBounds(250, 300, 120, 60);

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/button/BACK.png"))); // NOI18N
        jButton2.setBorderPainted(false);
        jButton2.setContentAreaFilled(false);
        jButton2.setDefaultCapable(false);
        jButton2.setFocusPainted(false);
        jButton2.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/button/BACKMouseOver.png"))); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton2);
        jButton2.setBounds(377, 300, 130, 60);

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel8.setText(":");
        jPanel1.add(jLabel8);
        jLabel8.setBounds(190, 220, 40, 30);

        MenuCodeBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        MenuCodeBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                MenuCodeBoxItemStateChanged(evt);
            }
        });
        MenuCodeBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MenuCodeBoxActionPerformed(evt);
            }
        });
        jPanel1.add(MenuCodeBox);
        MenuCodeBox.setBounds(230, 100, 190, 30);

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel9.setText("Old Menu Code");
        jPanel1.add(jLabel9);
        jLabel9.setBounds(60, 100, 130, 30);

        jLabel10.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel10.setText(":");
        jPanel1.add(jLabel10);
        jLabel10.setBounds(190, 100, 40, 30);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 520, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (MenuCodeBox.getSelectedIndex() == 0)
            JOptionPane.showMessageDialog(null, "Menu Code is not Selected", "Error!", JOptionPane.ERROR_MESSAGE);
        else if (menucode.getText().equals(""))
            JOptionPane.showMessageDialog(null, "Menu Code is not filled", "Error!", JOptionPane.ERROR_MESSAGE);
        else if (menuname.getText().equals(""))
            JOptionPane.showMessageDialog(null, "Menu Name is not filled", "Error!", JOptionPane.ERROR_MESSAGE);
        else if (menuprice.getText().equals(""))
            JOptionPane.showMessageDialog(null, "Menu Price is not filled", "Error!", JOptionPane.ERROR_MESSAGE);
        else{

    newcode = menucode.getText().toUpperCase();
    newname = menuname.getText().toUpperCase();
    newprice = menuprice.getText();
    String MenuCode = (String)this.MenuCodeBox.getSelectedItem();
    try {
            Main.getConnection();
            stmt = Main.con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
            rset = stmt.executeQuery("UPDATE Menu set Menu_Code ='"+newcode+"',Menu_Name = '"+newname+"',Menu_Price ='"+newprice+"' where menu_code = '"+MenuCode+"'");
            JOptionPane.showMessageDialog(null, "Update Success!!", "Success!", JOptionPane.INFORMATION_MESSAGE);
            Administrator.getdata();
            dispose();
        } catch (SQLException ex) {
            System.out.println("wrong action");
            JOptionPane.showMessageDialog(null, "Menu Could Not Been Changed", "Error!", JOptionPane.ERROR_MESSAGE);
        }
    }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        int reply = JOptionPane.showConfirmDialog(null, "Confirm Cancel Editing Menu?", "Confirm", JOptionPane.YES_NO_OPTION);
        if (reply == JOptionPane.YES_OPTION){
            dispose();
            reset();
        }
}//GEN-LAST:event_jButton2ActionPerformed

    private void MenuCodeBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MenuCodeBoxActionPerformed
        // TODO add your handling code here:

    }//GEN-LAST:event_MenuCodeBoxActionPerformed

    private void MenuCodeBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_MenuCodeBoxItemStateChanged
        String MenuCode = (String)this.MenuCodeBox.getSelectedItem();
        try {
            Main.getConnection();
            stmt = Main.con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
            rset = stmt.executeQuery("SELECT Menu_Code,Menu_Name,to_char(Menu_Price,'99,999.99') as Menu_Price from Menu where menu_code = '"+MenuCode+"'");
        } catch (SQLException ex) {
            System.out.println("wrong action");
        }

    try{

        while (rset.next()){
        oldcode = rset.getString("Menu_code");
        oldname = rset.getString("Menu_Name");
        oldprice = rset.getString("Menu_Price");
        }
        }
    catch (SQLException ex) {
            System.out.println("String Not available");
        }
        menucode.setText(oldcode);
        menuname.setText(oldname);
        menuprice.setText(oldprice);
    }//GEN-LAST:event_MenuCodeBoxItemStateChanged

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new EditMenuList().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox MenuCodeBox;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField menucode;
    private javax.swing.JTextField menuname;
    private javax.swing.JTextField menuprice;
    // End of variables declaration//GEN-END:variables

}
